#!/bin/bash

USAGE="Usage: duse [ base_dir ]
\tdisplay the disk space usage of the specified directory and each immediate subdirectory\n"
COL_W=8


if   [ "$1" == "" ] ; then base_dir=`pwd` ;
elif [ ! -d "$1"  ] ; then printf "$USAGE" ; exit ;
else                  base_dir=$1 ; fi ;

# get absolute path
path=`realpath "$base_dir" 2> /dev/null`
if [ "$path" == "" ] ; then path=$base_dir ; fi

# get results
while read line
do
  free_mb="$( echo $line | cut -d ' ' -f 1)" ; free_mbs=(  ${free_mbs[@]}  "$free_mb"  ) ;
  dir_name="$(echo $line | cut -d ' ' -f 2)" ; dir_names=( ${dir_names[@]} "$dir_name" ) ;
  total_dir_free_mb=$(( $total_dir_free_mb + $free_mb ))
done < <(du "$base_dir" --max-depth=1 --block-size=1 | sort -n)

# compute base dir use
n_dirs=${#dir_names[@]}
free_mbs[$n_dirs]=${free_mbs[$(( $n_dirs - 1 ))]}
dir_names[$n_dirs]="${dir_names[$(( $n_dirs - 1 ))]}/* (total)"
total_free_mb=${free_mbs[$n_dirs]}
base_dir_free_mb=$(( $total_free_mb - $(( $total_dir_free_mb - $total_free_mb )) ))
free_mbs[$(( $n_dirs - 1 ))]=$base_dir_free_mb
n_dirs=$(( $n_dirs + 1 ))

# display directory usage
printf "directory sizes under '$path'\n"
printf "MB used | dir\n"
printf "%s\n" "--------|----"
for (( dir_n=0 ; dir_n<$n_dirs ; dir_n++ ))
do
  free_mb=$(( ${free_mbs[$dir_n]} / 1024 / 1024 ))
  dir_name=${dir_names[$dir_n]}
  pad_w=$(( $COL_W - ${#free_mb} ))

  printf "%s%${pad_w}s| used in %s\n" "$free_mb" "" "$dir_name"
done

# display per partition free space
printf "\nfree space per partition\n"
printf "MB free | partition\n"
printf "%s\n" "--------|----------"
while read line
do
  device=`echo $line | awk '{ print $1 }'`
  free_mb=`echo $line | awk '{ print $4 }'`
  percent=`echo $line | awk '{ print $5 }'`
  mountpoint=`echo $line | awk '{ print $6 }'`
  pad_w=$(($COL_W - ${#free_mb}))
  printf "%s%${pad_w}s| %s free on %s => %s\n" $free_mb "" $percent $device $mountpoint
done < <(df --type=ext4 --block-size=1M | grep -v Avail)
